import { defineStore } from 'pinia'
import { login } from '@/common/api'
import type { AnyObject } from '@/common/types'

const useUserStore = defineStore('userStore', () => {
  const userId = ref('')
  const adUnitId = ref('')
  const singInDate = ref('')
  const playing = shallowRef<AnyObject>({})
  const searchHistory = ref<string[]>([])

  const userLogin = async(code: string, data? :AnyObject) => {
    const res = await login(code, data)
    userId.value = res.data
    uni.setStorageSync('orange-music-theatre-userid', res.data)
  }

  const setPlaying = (value: AnyObject) => {
    playing.value = value
    triggerRef(playing)
  }

  return {
    userId,
    adUnitId,
    singInDate,
    playing,
    searchHistory,
    userLogin,
    setPlaying
  }
})

export default useUserStore
